﻿using BCCommon;
using BCCommon.TMS.Transport;
using BCEntity.TMS.VehicleTransportBusinessConfig;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data;

namespace BCData.TMS.VehicleTransportBusinessConfig
{
    public class VehicleTransportBusinessConfigData : IVehicleTransportBusinessConfigData
    {
        private readonly IDatabaseContext database;

        public VehicleTransportBusinessConfigData(IDatabaseContext database)
        {
            this.database = database;
        }

        private const string QUERY = @"SELECT
		                                VehicleTransportBusinessConfigId,
		                                TransportBusiness,
                                        BusinessOrder,
                                        Remark,
                                        SameCityChargingMode,
                                        SameCityPointSurchargeAmount,
		                                Enabled,
		                                CreateAdminId,
		                                CreateAdminName,
		                                CreateTime,
		                                UpdateTime,
		                                EditAdminId,
		                                EditAdminName 
                                    FROM
                                        VehicleTransportBusinessConfig";

        public async Task<VehicleTransportBusinessConfigEntity> GetByTransportBusiness(TransportBusinessType transportBusiness)
        {
            return await database.Slave.QueryEntityAsync<VehicleTransportBusinessConfigEntity>($"{QUERY} WHERE TransportBusiness = ?TransportBusiness", new { TransportBusiness = transportBusiness.ToString() });
        }

        public async Task<VehicleTransportBusinessConfigEntity> Insert(VehicleTransportBusinessConfigEntity entity)
        {
            return await database.Master.InsertAsync(entity);
        }

        public async Task<(IEnumerable<VehicleTransportBusinessConfigViewEntity>, int)> Query(TransportBusinessType? transportBusiness, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (transportBusiness.HasValue)
            {
                wheres.Add("TransportBusiness = ?TransportBusiness");
                parameters.Add("?TransportBusiness", transportBusiness.Value.ToString(), DbType.AnsiString);
            }
            string sql = string.Format(@"{0} {1} ORDER BY VehicleTransportBusinessConfigId DESC LIMIT {2},{3};SELECT COUNT(1) FROM VehicleTransportBusinessConfig {1}",
                                QUERY,
                                wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty,
                                (pageIndex - 1) * pageSize,
                                pageSize);
            return await database.Slave.QueryPageableListAsync<VehicleTransportBusinessConfigViewEntity>(sql, parameters);
        }

        public async Task<VehicleTransportBusinessConfigEntity> GetEntity(int vehicleTransportBusinessConfigId)
        {
            return await database.Slave.QueryEntityAsync<VehicleTransportBusinessConfigEntity>($"{QUERY} WHERE VehicleTransportBusinessConfigId = ?VehicleTransportBusinessConfigId", new { vehicleTransportBusinessConfigId });
        }

        public async Task<VehicleTransportBusinessConfigEntity> Update(VehicleTransportBusinessConfigEntity entity)
        {
            return await database.Master.UpdateAsync(entity);
        }

        public async Task<int> Delete(int vehicleTransportBusinessConfigId)
        {
            return await database.Master.ExecuteAsync("DELETE FROM VehicleTransportBusinessConfig WHERE VehicleTransportBusinessConfigId = ?VehicleTransportBusinessConfigId", new { vehicleTransportBusinessConfigId });
        }

        public async Task<IEnumerable<VehicleTransportBusinessConfigViewEntity>> GetList()
        {
            string sql = string.Format("{0} WHERE Enabled=1", QUERY);
            return await this.database.Slave.QueryAsync<VehicleTransportBusinessConfigViewEntity>(sql);
        }

        public async Task<IEnumerable<VehicleTransportBusinessConfigEntity>> GetAllList(bool? enabled)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (enabled.HasValue)
            {
                wheres.Add("Enabled = ?Enabled");
                parameters.Add("?Enabled", enabled.Value, DbType.Boolean);
            }
            var sqlWhere = wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty;
            string sql = $"{QUERY}{sqlWhere}";
            return await this.database.Slave.QueryAsync<VehicleTransportBusinessConfigEntity>(sql, parameters);
        }
    }
}
